Skip to content

Fix #15080 FP uninitMemberVarNoCtor for const and reference members - #8910

Open
autoantwort wants to merge 4 commits into
cppcheck-opensource:mainfrom
autoantwort:fix-noctor-const-member
Open

autoantwort wants to merge 4 commits into
cppcheck-opensource:mainfrom
autoantwort:fix-noctor-const-member

Conversation

@autoantwort

Copy link
Copy Markdown
Contributor

https://trac.cppcheck.net/ticket/15080

struct S {
    const std::array<std::uint8_t, 2> a;
    const std::vector<std::uint8_t>::const_iterator it;
};
S f(const std::vector<std::uint8_t>& v) {
    return S{ {}, v.cbegin() };
}

gives Member variable 'S::it' has no initializer. [uninitMemberVarNoCtor], and struct S { int a = 0; const int b; int& c; }; warns for b and c.

A non-static const or reference member without default member initializer can't stay uninitialized: for trivial types the implicit default constructor is deleted, so every instance must initialize it, and a const class type is default-constructed. These members are now skipped by the no-constructor check.

Since f90a76f (#15072) this also shows up for class templates whose member type is a using alias.

This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.

🤖 Generated with Claude Code

A class without constructor whose non-static const or reference member has
no default member initializer is not default constructible (or, for a const
class type with a default constructor, default constructs it), so the member
can never be left uninitialized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread lib/checkclass.cpp Outdated
…or for all members

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread test/testconstructors.cpp
@autoantwort

Copy link
Copy Markdown
Contributor Author

Any further changes requested?

@danmar

danmar commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

@claude review

Comment thread lib/checkclass.cpp Outdated
@claude

claude Bot commented Oct 7, 2026

Copy link
Copy Markdown

The approach makes sense: a const member of trivial type, or a reference member, with no initializer deletes the implicit default constructor, so every instance has to be initialized another way.

One issue (inline): static const and static reference members are in the usage list too. The new check doesn't exclude them, so a declaration like static const int N; turns off uninitMemberVarNoCtor for the whole class. That's a new false negative. It needs a !var.isStatic() guard and a regression test.

Minor: it would be good to add a test for a const pointer member (int* const p;).

🤖 Generated with Claude Code

@danmar

danmar commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

please check the results here:
http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/

autoantwort and others added 2 commits October 8, 2026 14:47
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dmcppcheck

Copy link
Copy Markdown

Test results for commit d9ff8a4 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8910/
packages: 200
Differing warnings: 31 (+0 -31)
AI review: 22 improvement, 8 regression, 1 unclear (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8910/2026-10-08_12-49-15-d9ff8a48bccd-review.txt)
Timing: -0.1% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8910/2026-10-08_12-49-15-d9ff8a48bccd_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants